package com.songlanyun.modules.order.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.order.entity.ReservationRecordEntity;
import com.songlanyun.modules.order.model.dto.ReservationRecordDTO;
import com.songlanyun.modules.order.service.ReservationRecordService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 服务商品预约记录
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-03-19 13:54:17
 */
@RestController
@RequestMapping("admin/service/goods/reservation/record")
public class ReservationRecordAdminController extends AbstractController {
    @Autowired
    private ReservationRecordService reservationRecordService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("reservationrecord:reservationrecord:list")
    public R list(@RequestParam Map<String, Object> params) {
        setShopId(params);
        PageUtils page = reservationRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("reservationrecord:reservationrecord:info")
    public R info(@PathVariable("id") Long id) {
        ReservationRecordEntity reservationRecord = reservationRecordService.getById(id);

        return R.ok().put("data", reservationRecord);
    }

    /**
     * 预约
     */
    @SysLog("保存服务商品预约记录")
    @PostMapping("/save")
    //@RequiresPermissions("reservationrecord:reservationrecord:save")
    public R save(@RequestBody ReservationRecordDTO dto) {
        ValidatorUtils.validateEntity(dto);
        //预约时间需要在当前时间之后
        if (System.currentTimeMillis() > dto.getAppointmentTime().getTime()) {
            throw new RRException(OrderException.APPOINTMENT_TIME_GT_NEW);
        }

        dto.setRegistrant(getUserId());
        dto.setShopId(getShopId(true));
        reservationRecordService.forward(dto);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除服务商品预约记录")
    @DeleteMapping("/delete/{id}")
    //@RequiresPermissions("reservationrecord:reservationrecord:delete")
    public R delete(@PathVariable("id") Long id) {
        reservationRecordService.removeById(id);

        return R.ok();
    }

}
